<?php
namespace Yzadmin\Controller;
use Common\Controller\AdminBaseController;
/**
 * 后台文章控制器
 */
class ArticleController extends AdminBaseController {

    /*文章管理首页*/
    public function index(){
      $catid=intval(I('catid'));$q=I('q');$keywords=I('keywords');
      $map = array();
      if (!empty($catid)){$map['catid'] = $catid;}
      if (!empty($q)){$map['title']=array('like','%'.$q.'%');}
      $count = D('Article')->where($map)->count();
      $Page = new \Think\Page($count,15);
      $lists = D('Article')->where($map)->order('sort asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      if(!empty($q)){$Page->parameter['q']=$q;}
      if(!empty($catid)){$Page->parameter['catid']=$catid;}
      $show = $Page->show();// 分页显示输出
      foreach($lists as $key=>$val){
        if($val['status'] == 1){
          $lists[$key]['status'] = '审核通过';
        }else{
          $lists[$key]['status'] = '<span class="red-error">未通过</span>';
        }
      }
      $map_category = array('type'=>'Article');
      $assign=array(
            'lists'=>$lists,
            'catid'=>$catid,
            'keywords'=>$q,
            'page'=>$show,
            'category'=>D('Category')->getAll(),
            'categorys'=>D('Category')->where(array('type'=>'Article'))->select()
            );
      $this->assign($assign);
      $this->display();
    }

    /*文章管理添加*/
    public function add(){
      if (IS_POST){
        $data = I('post.data');
        $data['create_time'] = strtotime($data['create_time']);
        $data['adminid'] = is_login();
        if(empty($data['is_recom'])){
          $data['is_recom'] = 0;
        }
        if(empty($data['status'])){
          $data['status'] = 0;
        }
        $result = D('Article')->add($data);
        if(false !== $result){
          $this->success('数据操作成功！',U('index'));exit;
        }else{
          $this->error('服务器正忙，请稍后再试！');
        }
      }
      $map_category = array('type'=>'Article');
      $category_tree = D('Category')->formatTree($map_category);
      $assign=array(
            'category_tree'=>$category_tree
            );
      $this->assign($assign);
      $this->display('edit');
    }

    /*文章管理修改*/
    public function edit(){
      if (IS_POST){
        $data = I('post.data');$id = intval(I('post.id'));
        if(empty($id)){$this->error('服务器正忙，请稍后再试！');}
        $data['create_time'] = strtotime($data['create_time']);
        $data['adminid'] = is_login();
        if(empty($data['is_recom'])){
          $data['is_recom'] = 0;
        }
        if(empty($data['status'])){
          $data['status'] = 0;
        }
        $result = D('Article')->where(array('id'=>$id))->save($data);
        if(false !== $result){
          $this->success('数据操作成功！',U('index'));exit;
        }else{
          $this->error('服务器正忙，请稍后再试！');
        }
      }
      $id=I('get.id');if(empty($id)){$this->error('非法操作!');}
      $info = D('Article')->find($id);if(empty($info)){$this->error('信息不存在!');}
      $info['create_time'] = date('Y-m-d H:i:s',$info['create_time']);
      $map_category = array('type'=>'Article');
      $category_tree = D('Category')->formatTree($map_category);
      $assign=array(
            'info'=>$info,
            'category_tree'=>$category_tree
            );
      $this->assign($assign);
      $this->display();
    }

    /*批量操作*/
    public function batch($ids = 0,$action=0){
      if(IS_POST){
        if(empty($ids) || empty($action)){echo 0;exit;}
        if($action == 1){//审核通过
          $data['status'] = 1;
          foreach ($ids as $val){
            $result = D('Article')->where('id='.$val)->save($data);
          }
        }
        if($action == 2){//取消审核
          $data['status'] = 0;
          foreach ($ids as $val){
            $result = D('Article')->where('id='.$val)->save($data);
          }
        }
        if($action == 3){//热门推荐
          $data['is_recom'] = 1;
          foreach ($ids as $val){
            $result = D('Article')->where('id='.$val)->save($data);
          }
        }
        if($action == 4){//取消推荐
          $data['is_recom'] = 0;
          foreach ($ids as $val){
            $result = D('Article')->where('id='.$val)->save($data);
          }
        }
        if($action == 5){//删除
          foreach ($ids as $val){
            $result = D('Article')->where('id='.$val)->delete();
          }
        }
        echo 1;exit;
      }
    }

    /*文章删除操作*/
    function del($id = 0){
      if(IS_POST){
        if(empty($id)){echo 0;exit;}
        $result = D('Article')->where('id='.$id)->delete();
        if($result){
          echo 1;exit;
        }else{
          echo 0;exit;
        }
      }
    }


}
